
//index.js
//获取应用实例
var tcity = require("../../utils/citys.js");
var app = getApp()
Page({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    area: [],
    flag: 0,
    openid: app.globalData.openid,
    condition: false
  },
  open: function (e) {
    
    if (this.data.flag == 0) {
      // console.log(e);
      // console.log('e.detail.value'+e.detail.value);
      this.setData({
        condition: !this.data.condition,
        area: e.detail.value,
        flag: 1
      })
    }else{
      var add = this.data.province + this.data.city + this.data.county
      this.setData({
        condition: !this.data.condition,
        area: add,
      })
    }
  },
  c: function (c) {
    this.setData({ flag: true })
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/trueaddress',
      data: {
        aid: c.currentTarget.dataset.aid,
        aopenid: app.globalData.openid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.switchTab({
          url: '../index/index'
        })
      }
    })
  },
  b: function (b) {
    this.setData({ flag: true })
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/deladdress',
      data: {
        aid: b.currentTarget.dataset.aid,
        openid:app.globalData.openid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.redirectTo({

          url:"../adress/adress",
        })
      }
    })
  },
  bindChange: function (e) {
    //console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }
      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })
      return;
    }
    if (val[1] != t[1]) {
      // console.log('city no');
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }
      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }
  },
  formSubmit: function (e) {
    console.log(this.data.area);
    console.log('form发生change事件，携带value值为：', e.detail.value.ausername);
    if (e.detail.value.ausername.length == 0 || e.detail.value.amobile.length == 0 || e.detail.value.addresss.length == 0) {
      wx.showToast({
        title: '收货人姓名、联系电话、详细地址,不能为空',
        icon: 'fail',
        duration: 2000,
        image: '../../images/tanhao.png'
      });
    } else {
      wx.request({
        url: getApp().data.apiurl + '/index.php/index/index/updateaddress',
        data: {
          aid: e.detail.value.aid,
          ausername: e.detail.value.ausername,
          amobile: e.detail.value.amobile,
          area: e.detail.value.pcc,
          addresss: e.detail.value.addresss,
          openid: app.globalData.openid
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          wx.navigateBack(1);
          that.setData({
            carts: res.data.data
          })
        }
      })
    }
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '编辑地址'
    })
    // console.log("onLoad");
    var that = this;
    that.setData({
      aid: options.aid,
    });
    //获取用户地址
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/updaddress',
      data: {
        aid:options.aid,
        aopenid: app.globalData.openid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          address: res.data,
        });
      }
    });
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];
    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    // console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    // console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
    // console.log('初始化完成');
  }
})

